package com.yunshan.mp;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yunshan.common.annotation.Log;
import com.yunshan.common.core.controller.BaseController;
import com.yunshan.common.core.domain.AjaxResult;
import com.yunshan.common.enums.BusinessType;
import com.yunshan.mp.domain.MpIndexPreview;
import com.yunshan.mp.service.IMpIndexPreviewService;
import com.yunshan.common.utils.poi.ExcelUtil;
import com.yunshan.common.core.page.TableDataInfo;

/**
 * 预览页配置Controller
 * 
 * @author yunshan
 * @date 2025-01-20
 */
@Api(tags = "预览页配置")
@RestController
@RequestMapping("/mp/preview")
public class MpIndexPreviewController extends BaseController
{
    @Autowired
    private IMpIndexPreviewService mpIndexPreviewService;

    /**
     * 查询预览页配置列表
     */
    @ApiOperation("查询预览页配置列表")
    @GetMapping("/list")
    public TableDataInfo list(MpIndexPreview mpIndexPreview)
    {
        startPage();
        List<MpIndexPreview> list = mpIndexPreviewService.selectMpIndexPreviewList(mpIndexPreview);
        return getDataTable(list);
    }

    /**
     * 导出预览页配置列表
     */
    @ApiOperation("导出预览页配置列表")
    @Log(title = "预览页配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MpIndexPreview mpIndexPreview)
    {
        List<MpIndexPreview> list = mpIndexPreviewService.selectMpIndexPreviewList(mpIndexPreview);
        ExcelUtil<MpIndexPreview> util = new ExcelUtil<MpIndexPreview>(MpIndexPreview.class);
        util.exportExcel(response, list, "预览页配置数据");
    }

    /**
     * 获取预览页配置详细信息
     */
    @ApiOperation("获取预览页配置详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(mpIndexPreviewService.selectMpIndexPreviewById(id));
    }

    /**
     * 新增预览页配置
     */
    @ApiOperation("新增预览页配置")
    @Log(title = "预览页配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MpIndexPreview mpIndexPreview)
    {
        return toAjax(mpIndexPreviewService.insertMpIndexPreview(mpIndexPreview));
    }

    /**
     * 修改预览页配置
     */
    @ApiOperation("修改预览页配置")
    @Log(title = "预览页配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MpIndexPreview mpIndexPreview)
    {
        if (mpIndexPreview.getId() == null){
            return toAjax(mpIndexPreviewService.insertMpIndexPreview(mpIndexPreview));
        }

        return toAjax(mpIndexPreviewService.updateMpIndexPreview(mpIndexPreview));
    }

    /**
     * 删除预览页配置
     */
    @ApiOperation("删除预览页配置")
    @Log(title = "预览页配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mpIndexPreviewService.deleteMpIndexPreviewByIds(ids));
    }

    /**
     * 查询预览页配置
     */
    @ApiOperation("查询预览页配置")
    @GetMapping("/query")
    public AjaxResult query(MpIndexPreview mpIndexPreview)
    {
        mpIndexPreview.setStatus(0);
        mpIndexPreview.setIsDeleted(0);
        MpIndexPreview mpIndex = new MpIndexPreview();

        List<MpIndexPreview> list = mpIndexPreviewService.selectMpIndexPreviewList(mpIndexPreview);

        if (list.size() > 0){
            mpIndex = list.get(0);
        }

        return success(mpIndex);
    }
}
